
const { ccclass, property } = cc._decorator;

@ccclass
export class load extends cc.Component {

    assetDic = {}   // 所有资源
    bundleDic = {}   // 包资源
    loadCount: 0
    loading_list: String[] = []
    currentCount: 0
    // 读取 没有便加载 完成执行回调
    getRes(url, type, func, bundle) {
        if (!type) {
            type = cc.Prefab
        }

        let asset
        if (bundle) {
            if (this.bundleDic[bundle]) {
                asset = this.bundleDic[bundle].load(url, type);
            } else {
                console.error("你特么包都没整明白，害搁着调取，梁静茹给你的勇气吗？")
                return null
            }
        } else {
            asset = cc.resources.load(url, type);
        }
        if (asset) {
            this.assetDic[url + '/' + type.name] = asset
            asset.load_url = url
            if (func) {
                func(null, asset)
            }
        } else {
            let res_bundle
            if (bundle) {
                res_bundle = this.bundleDic[bundle]
            } else {
                res_bundle = cc.resources
            }
            this.loadCount += 1
            this.loading_list.push(url)
            res_bundle.load(url, type, (err, res) => {
                if (this.loading_list.indexOf(url) >= 0) {
                    this.loading_list.splice(this.loading_list.indexOf(url), 1)
                }
                this.assetDic[url + '/' + type.name] = res
                this.currentCount += 1
                if (err) {
                    console.error(err)
                }
                if (func) {
                    if (res) {
                        res.load_url = url
                    }
                    func(err, res)
                }
            });
        }
    }
    // 读取 return 存在内容
    get(url, type, bundle) {
        if (!type) {
            type = cc.Prefab
        }
        if (this.assetDic[url + '/' + type.name]) {
            return this.assetDic[url + '/' + type.name]
        }
        let asset
        if (bundle) {
            if (this.bundleDic[bundle]) {
                asset = this.bundleDic[bundle].load(url, type);
            } else {
                console.error("你特么包都没整明白，害搁着调取，梁静茹给你的勇气吗？")
                return null
            }
        } else {
            asset = cc.resources.load(url, type);
        }
        if (asset) {
            this.assetDic[url + '/' + type.name] = asset
            asset.load_url = url
            return asset
        } else {
            return null
        }
    }
    // 加载资源包
    loadBundle(url, func, key) {
        if (url && key && (this.bundleDic[key] || this.bundleDic[url])) {
            if (this.bundleDic[key]) {
                func(null, this.bundleDic[key])
                return
            }
            if (this.bundleDic[url]) {
                func(null, this.bundleDic[url])
                return
            }
        }
        this.loadCount += 1
        this.loading_list.push(url)
        cc.assetManager.loadBundle(url, (err, bundle) => {
            if (err) {
                console.error(err)
                return
            }
            if (this.loading_list.indexOf(url) >= 0) {
                this.loading_list.splice(this.loading_list.indexOf(url), 1)
            }
            this.currentCount += 1
            if (key) {
                this.bundleDic[key] = bundle
            } else {
                this.bundleDic[url] = bundle
            }
            if (func) {
                func(err, bundle)
            }
        });
    }
    // 强行加载 然后回调
    loadRes(url, type, func, bundle) {
        if (!url || !type) {
            return
        }
        if (type == cc.SceneAsset) {
            this.loadScene(url, func)
            return
        }
        this.loadCount += 1
        this.loading_list.push(url)

        let res_bundle
        if (bundle) {
            res_bundle = this.bundleDic[bundle]
        } else {
            res_bundle = cc.resources
        }

        res_bundle.load(url, type, (err, res) => {
            if (this.loading_list.indexOf(url) >= 0) {
                this.loading_list.splice(this.loading_list.indexOf(url), 1)
            }
            this.assetDic[url + '/' + type.name] = res
            this.currentCount += 1
            if (err) {
                console.error(err)
            }
            if (func) {
                func(err, res)
            }
        });
    }
    // 强制加载 列表
    loadResList(list, func) {
        for (let i = 0; i < list.length; i++) {
            if (list[i] && list[i].url && list[i].type) {
                this.loadRes(list[i].url, list[i].type, func, list[i].bundle)
            }
        }
    }
    // 检查整体有加载完成的内容没
    loadOver() {
        if (this.loadCount > 0 && this.currentCount > 0) {
            if (this.loadCount == this.currentCount) {
                return true
            }
            return false
        }
        return true
    }
    checkLoadOver(list) {
        let loading_list = []
        for (let i = 0; i < list.length; i++) {
            if (this.loading_list.indexOf(list[i].url) >= 0) {
                loading_list.push(list[i].url)
            }
        }
        if (loading_list.length) {
            // he.log("未加载状态",loading_list)
            return false
        } else {
            return true
        }
    }
    // 加载进度
    loadStatus() {
        if (this.currentCount) {
            return parseFloat((this.currentCount / this.loadCount).toFixed(2))
        } else {
            return 0
        }
    }
    // 预加载场景
    loadScene(name, func) {
        this.loadCount += 1
        cc.resources.loadScene(name, (err, scene) => {
            this.currentCount += 1
            if (func) {
                func(err, scene)
            }
            console.log(`load scene_${name} successfully.`);
        });
    }
    // 释放资源
    release(url, type) {
        if (this.assetDic[url + '/' + type.name]) {
            cc.loader.release(this.assetDic[url + '/' + type.name])
            delete this.assetDic[url + '/' + type.name]
        } else {
            console.warn('释放未找到对应资源')
        }
    }
    // 强行释放资源，关联素材也会一并删除
    // prefab预制体一般用这个释放
    // 注意！预制体的素材如果在多处 使用会产生空缺Bug 直接死机
    dependRelease(url, type) {
        if (this.assetDic[url + '/' + type.name]) {
            cc.loader.release(cc.loader.getDependsRecursively(this.assetDic[url + '/' + type.name]))
            delete this.assetDic[url + '/' + type.name]
        } else {
            console.warn('释放未找到对应资源')
        }
    }
}
